using System;
using System.Collections.Generic;
using System.Linq;
using MetaObjects.Contracts;

namespace MetaObjects.Core
{
	public class MetaEntity<TEntity> : IMetaEntity<TEntity>, IEntity<string>
	{
		public MetaEntity(string internalName)
			: this(internalName, () => (TEntity)Activator.CreateInstance(typeof(TEntity)), Enumerable.Empty<IMetaField>())
		{
		}

		public MetaEntity(string internalName, Func<TEntity> factory, IEnumerable<IMetaField> columns)
		{
			InternalName = internalName;
			MetaFields = new EntityCollection<IMetaField>(columns);
			Factory = factory;
		}

		public string InternalName { get; private set; }
		public string DisplayName { get; set; }
		public string Description { get; set; }
		public IEntityCollection<IMetaField> MetaFields { get; private set; }
		Func<object> IMetaEntity.Factory
		{
			get { return () => Factory();  }
		}
		public Func<TEntity> Factory { get; set; }
		//Func<TEntity, object> IMetaEntity<TEntity>.KeyAccessor { get; set; }
		//Func<object, object> IMetaEntity.KeyAccessor { get; set; }
		object IEntity.Key { get { return InternalName; } }
		string IEntity<string>.Key { get { return InternalName; } }
	}
}